Page({
    data: {
        todoText: '',
        todoList: ['吃饭饭', '睡觉觉']
    },
    onSubmit() {
        const list = this.data.todoList;
        const todoText = this.data.todoText.trim();
        if (!todoText) {
            wx.showToast({
                title: '不能为空哦',
                icon: 'none'
            });
            this.setData({ todoText: '' }); // 清空输入框
            return;
        }
        if (list.includes(todoText)) {
            wx.showToast({
                title: '已经记下了',
                icon: 'none'
            });
            this.setData({ todoText: '' }); // 清空输入框
            return;
        }
        list.push(todoText);
        this.setData({
            todoList: list,
            todoText: ''
        });
    },

    delItem(e) {
        const list = this.data.todoList;
        const index = e.mark.index; // 确保索引是正确的
        if (index >= 0 && index < list.length) { // 检查索引是否有效
            list.splice(index, 1); // 删除特定索引的元素
            this.setData({
                todoList: list,
            });
        }
    },

    delAll() {
        this.setData({
            todoList: [],
        })
    }

})